//
//  LotteryTypeView.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/6.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "LotteryTypeView.h"
#import "LabCell.h"
#import "Home_TypeView.h"
#import "LotteryTypeModel.h"
#import "NewsLottryTypeModel.h"
#import "GalleryModel.h"
@interface LotteryTypeView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) Home_TypeView   *home_TypeView;
@end

@implementation LotteryTypeView{
    UIImageView *arrImage;
}

//- (instancetype)init{
//    if (self=[super init]) {
//        self.dataArr=[BaseVM manage].lotteryArr;
//    }
//    return self;
//}


- (void)setDataArr:(NSArray *)dataArr{
    _dataArr=dataArr;
    
    [self.collectionView reloadData];
}

- (void)onclickMore{
    WS(weakSelf);
    
    if (self.home_TypeView) {
        [self.home_TypeView remove];
        self.home_TypeView=nil;
        arrImage.image=[UIImage imageNamed:@"arrow_bottom"];
        return;
    }
    arrImage.image=[UIImage imageNamed:@"arrow_top"];
    self.home_TypeView=[[Home_TypeView alloc]init];
    
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *bgView=[TGTools viewController:self].view;
    [bgView addSubview:self.home_TypeView];
    [self.home_TypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(bgView);
        make.top.equalTo(self.mas_bottom);
    }];

    
    [self.home_TypeView setArr:self.dataArr andIndex:weakSelf.selectLottery andSelectBlock:^(NSInteger row) {
        [weakSelf updateRow:row];
        
    }];
}


- (void)updateRow:(NSInteger)row{
    self.selectLottery=row;
    self.scrollIndex=row;
    self.home_TypeView=nil;
}

- (void)setSelectLottery:(NSInteger)selectLottery{
    
    _selectLottery=selectLottery;
    if (self.chaneBlock) {
        self.chaneBlock(self.selectLottery);
    }
    if (self.home_TypeView) {
//        [self.home_TypeView setSelectType:_selectLottery];
        [self.home_TypeView removeFromSuperview];
        self.home_TypeView = nil;
    }
    [self.collectionView reloadData];
}

- (void)setScrollIndex:(NSInteger)scrollIndex{
    
    if (self.dataArr.count==0) {
        return;
    }
    
    [self layoutIfNeeded];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:scrollIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    
    if ([self.dataArr[indexPath.row] isKindOfClass:[LotteryTypeModel class]]) {
        LotteryTypeModel *model=self.dataArr[indexPath.row];
        [cell setTitle:model.Name andIsSelect:indexPath.row==self.selectLottery];
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[NewsTypeModel class]]){
        NewsTypeModel *model=self.dataArr[indexPath.row];
        [cell setTitle:model.TypeName andIsSelect:indexPath.row==self.selectLottery];
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[GalleryExtraModel class]]){
        GalleryExtraModel *model=self.dataArr[indexPath.row];
        [cell setTitle:[NSString stringWithFormat:@"第%@期",model.Issue] andIsSelect:indexPath.row==self.selectLottery];
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[NewsLottryTypeModel class]]){
        NewsLottryTypeModel *model=self.dataArr[indexPath.row];
        [cell setTitle:model.LTypeName andIsSelect:indexPath.row==self.selectLottery];
    }
    [cell.labTitle setFont:[UIFont boldSystemFontOfSize:15]];
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.dataArr[indexPath.row] isKindOfClass:[LotteryTypeModel class]]) {
        return CGSizeMake((SCREEN_WIDTH-40)/6+10, 40);
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[NewsTypeModel class]]){
        return CGSizeMake((SCREEN_WIDTH-40)/4+10, 40);
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[GalleryExtraModel class]]){
        return CGSizeMake((SCREEN_WIDTH-40)/4+10, 40);
    }else if ([self.dataArr[indexPath.row] isKindOfClass:[NewsLottryTypeModel class]]){
        return CGSizeMake((SCREEN_WIDTH-40)/5+10, 40);
    }
    return CGSizeMake((SCREEN_WIDTH-40)/4+10, 40);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selectLottery=indexPath.row;
    
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        WS(weakSelf);
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-40)/4, 40)];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator=NO;
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf).with.insets(UIEdgeInsetsMake(0, 0, 0, 50));
            
        }];
        
        UIImageView *shadowImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_shadow"]];
        shadowImage.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickMore)];
        [shadowImage addGestureRecognizer:tap];
        [self addSubview:shadowImage];
        [shadowImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.collectionView.mas_right)
            .with.offset(-10);
            make.top.right.bottom.equalTo(weakSelf);
        }];
        
        arrImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_bottom"]];
        [self addSubview:arrImage];
        [arrImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(shadowImage);
            make.centerX.equalTo(shadowImage).with.offset(3);
            make.size.mas_equalTo(arrImage.image.size);
        }];
        
        
        UIView *lineView=[[UIView alloc]init];
        lineView.backgroundColor=DEFAULTBGCOLOR;
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(@.7);
        }];
        
        
    }
    return _collectionView;
}

@end
